package pub.tbc.mybatis.controller;

import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pub.tbc.mybatis.assist.plugin.page.PageParams;
import pub.tbc.mybatis.assist.plugin.page.PageWrapper;
import pub.tbc.mybatis.domain.User;
import pub.tbc.mybatis.mappers.UserMapper;

import java.util.List;
import java.util.Map;

/**
 * @author tbc on 2017/5/17 15:17:20.
 */
@Slf4j
@RestController
public class MyController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("list")
    public Object pageUserList(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize
    ) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;

        PageParams pageParams = PageParams.of(pageNum, pageSize);
        List<User> list = userMapper.pageUsers((Map) ImmutableMap.of("pageParam", pageParams));
        return new PageWrapper(pageParams, list);
    }
}
